<template></template>

<script setup>
import axios from 'axios'
import { useUserStore } from '@/stores/authStores'
const userStore = useUserStore();

const isLoading = ref(false);
const isSignInDisabled = ref(false);
// 表单数据
const refLoginForm = ref()
const email = ref('177131293@qq.com')
const password = ref('12345678')
const isFormValid = ref(true);

const emailRules = [
  v => !!v || 'E-mail is required', // 检查电子邮件是否为空
  v => /.+@.+\..+/.test(v) || 'E-mail must be valid', // 检查电子邮件格式
];

const passwordRules = [
  v => !!v || 'Password is required', // 检查密码是否为空
  v => (v && v.length >= 8) || 'Password must be at least 8 characters', // 密码长度至少8个字符
];

const visible = ref('true')

const handleLogin = async () => {
  const { valid } = await refLoginForm.value.validate();
  if (valid) {
    isLoading.value = true;
    isSignInDisabled.value = true;
    try {
      const response = await loginUser(email.value, password.value);
      console.log("登录成功", response);
      // 你可以在这里处理响应，比如保存 token
    } catch (error) {
      console.error("登录失败", error);
    } finally {
      isLoading.value = false;
      isSignInDisabled.value = false;
    }
  } else {
    console.log("表单验证失败");
  }
};

// 添加一个新的函数用于发送登录请求
async function loginUser(email, password) {
  const response = await axios.post('http://localhost:1440/api/auth/local', {
    identifier: email,
    password: password
  });
  return response.data;

}

</script>

<style scoped>
.form {
  margin: 0 auto;
  min-width: 400px;
  min-height: auto;
}
</style>
